# -*- coding: UTF-8 -*-
# 作者： 磊
# @Time：2022/5/4
# coding=utf-8
import re
import pytest as pytest
import requests

class TestSecondRequest3:
    access_token = ""
    csrf_token = ""
    session=requests.session()  #获取回话对象，不去获取cookies

    def test_phpwind_start(self):
        url = "http://47.107.116.139/phpwind/"
        res = TestSecondRequest3.session.request("get",url=url)
        return_data = res.text
        obj = re.search('name="csrf_token" value="(.*?)"', return_data)
        TestSecondRequest3.csrf_token = obj.group(1)
        TestSecondRequest3.phpwind_cookie = res.cookies

    # 成功不了，密码有误
    def test_login(self):
        url = "http://47.107.116.139/phpwind/index.php?m=u&c=login&durun"
        data = {
            "username": "msxy",
            "password": "msxy",
            "csrf_token": TestSecondRequest3.csrf_token,
            "backurl": "http://47.107.116.139/phpwind/",
            "invite": ""
        }
        headers = {
            "Accept": "application/json, text/javascript, */*; q=0.01",
            "X-Requested-With": "XMLHttpRequest"
        }
        # 请求的时候要带上cookies，否则无状态登录
        res = TestSecondRequest3.session.request("post",url=url, data=data, headers=headers, cookies=TestSecondRequest3.phpwind_cookie)
        print("请求头：", res.request.headers)
        print(res.text)


if __name__ == '__main__':
    pytest.main(['-vs', "test04_session.py"])